package com.boss.salary.parsingExcel.impl;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.boss.salary.ExcelModel.ExcelResult;
import com.boss.salary.ExcelModel.PersonInfoBaseDataModel;
import com.boss.salary.parsingExcel.ExcelParsingService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <br>Title:
 * <br>Description:
 * <br>Author:
 * <br>Date:
 */
@Service("personInfoBaseDataExcelParsingServiceImpl")
public class PersonInfoBaseDataExcelParsingServiceImpl implements ExcelParsingService {
    @Override
    public <E> ExcelResult<E> readExcelWithModel(InputStream inputStream, Class<? extends BaseRowModel> clazz, ExcelTypeEnum excelTypeEnum, UserDTO userDto) {
        // 调用listener
        PersonInfoBaseDataExcelParsingServiceImpl.PersonExcelParsingListener listener = new PersonInfoBaseDataExcelParsingServiceImpl.PersonExcelParsingListener();
        ExcelReader excelReader = new ExcelReader(inputStream, excelTypeEnum, null, listener);
        //默认只有一列表头
        excelReader.read(new Sheet(1, 2, clazz));
        List<PersonInfoBaseDataModel> list = listener.getSuccessList();
        ExcelResult<PersonInfoBaseDataModel> excelResult = new ExcelResult();
        excelResult.setSuccessList(list);
        return (ExcelResult<E>) excelResult;
    }

    /**
     * excel解析
     * @param
     */
    private class PersonExcelParsingListener extends AnalysisEventListener<PersonInfoBaseDataModel> {

        /** 成功list **/
        private List<PersonInfoBaseDataModel> successList = new ArrayList<>();

        @Override
        public void invoke(PersonInfoBaseDataModel object, AnalysisContext analysisContext) {
            successList.add(object);
            //根据自己业务做处理
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

        public List<PersonInfoBaseDataModel> getSuccessList() {
            return successList;
        }

        public void setSuccessList(List<PersonInfoBaseDataModel> successList) {
            this.successList = successList;
        }

    }
}
